var modSample2_locLang=new localLang("modSample2");
var modSample2 = new Class({
	Extends:WFWindow,
	sampleProxyEnable: false,	
    options: {
        name: 'modSample2',
        title: modSample2_locLang.label('title'),
        btnSettings: true,
        btnRefresh: true,
        btnHelp: true,
        btnClose: true,
        loadCSS: true,
		supportServerTypes:['modSimple'],
        defaultWidgetHeight: 244
    },
    includeScript: ['adapter.js'],
    settingModalOption:{
        title: modSample2_locLang.label('edit') + ' - '+ modSample2_locLang.label('title'),
        content: '<input type="button" class="cancel" value="'+modSample2_locLang.label('btn_cancel')+'">',
        callBack: function(){
            $$('.modalContent .cancel').addEvent('click',function(e){
                this.refreshMod();
                this.settingModal.close();
            }.bind(this));
        }
    },
    renderContent: function(){
        this.errormsg = new Element('div',{'class':'error'}).inject(this.boxBD);
        this.msg = new WFMessage(this.errormsg);
        var option = {
            type: '0', //0: error, 1: warn, 2: info
            description: 'aaa', //text
            button: 'aaa', //string
            callback: function(){alert('1');}, //callback function
            close: true //true or false      
        };
        var option2 = {
            type: '1', //0: error, 1: warn, 2: info
            description: 'bbb', //text
            button: 'bbb', //string
            callback: function(){alert('2');}, //callback function
            close: true //true or false        
        };
        //this.msg.fireEvent('message',option);
        //this.msg.fireEvent('message',option2);
        //this.addButton('btn_refresh',this.onClickRefresh2.bind(this));
        //this.fillTitle('title');
        this.chartBox = new Element('div', {'class':'container'}).injectInside(this.boxBD);
        this.width = this.chartBox.getSize().x;
        this.height = 300;
        this.swf = 'Pie3D.swf';//Pie3D.swf //Column3D.swf
        var data = "module=modSimple&url="+encodeURIComponent(this.widgetPath+'/sampleReporting.php?fakename=fake.xml');
		if(this.sampleProxyEnable == true)
		    this.proxyRequest(data,this.displaySuccess.bind(this),this.displayFailure.bind(this));
		else
			this.displaySuccess();
    },
	onClickRefresh2: function(){
		var option = {
			title: 'customize button',
			content: '<input type="button" class="cancel" value="'+modSample2_locLang.label('btn_cancel')+'">',
			callBack: function(){
				$$('.modalContent .cancel').addEvent('click',function(e){
					this.customButtonModal.close();
				}.bind(this));
			}.bind(this)
		};
		this.customButtonModal = new WFModal(option);
	},
    displaySuccess: function(response)
	{
        //parse JSON to XML
        if(this.swf=='Bubble.swf' && this.sampleProxyEnable == false )
		{
            this.chart_data = [
                '<chart palette="3" numberPrefix="$" is3D="1" animation="1" clipBubbles="1" xAxisMaxValue="100" showPlotBorder="0" xAxisName="Stickiness" yAxisName="Cost Per Service" chartRightMargin="30">',
                    '<categories>',
                        '<category label="0%" x="0"/>',
                        '<category label="20%" x="20" showVerticalLine="1"/>',
                        '<category label="40%" x="40" showVerticalLine="1"/>',
                        '<category label="60%" x="60" showVerticalLine="1"/>',
                        '<category label="80%" x="80" showVerticalLine="1"/>',
                        '<category label="100%" x="100" showVerticalLine="1"/>',
                    '</categories>',
                    '<dataSet showValues="0">',
                        '<set x="30" y="1.3" z="116" name="Traders"/>',
                        '<set x="32" y="3.5" z="99" name="Farmers"/>',
                        '<set x="8" y="2.1" z="33" name="Individuals"/>',
                        '<set x="62" y="2.5" z="72" name="Medium Business Houses"/>',
                        '<set x="78" y="2.3" z="55" name="Corporate Group A"/>',
                        '<set x="75" y="1.4" z="58" name="Corporate Group C"/>',
                        '<set x="68" y="3.7" z="80" name="HNW Individuals"/>',
                        '<set x="50" y="2.1" z="105" name="Small Business Houses"/>',
                    '</dataSet>',
                    '<trendlines>',
                        '<line startValue="2.5" isTrendZone="0" displayValue="Median Cost" color="0372AB"/>',
                    '</trendlines>',
                    '<vTrendlines>',
                        '<line startValue="0" endValue="60" isTrendZone="1" displayValue="Potential Wins" color="663333" alpha="10"/>',
                        '<line startValue="60" endValue="100" isTrendZone="1" displayValue="Cash Cows" color="990099" alpha="5"/>',
                    '</vTrendlines>',
                '</chart>'
            ].join(',');
        }
        else if(this.sampleProxyEnable == false )
		{
            this.chart_data = [
                '<chart palette="2" caption="Unit Sales" xAxisName="Month" yAxisName="Units" showValues="0" decimals="0" formatNumberScale="0" useRoundEdges="1">',
                    '<set label="Jan" value="123" />',
                    '<set label="Feb" value="857" />',
                    '<set label="Mar" value="671" />',
                    '<set label="Apr" value="494" />',
                    '<set label="May" value="761" />',
                    '<set label="Jun" value="960" />',
                	'<set label="Jul" value="523" />',
                	'<set label="Aug" value="202" />',
                	'<set label="Sep" value="621" />',
                	'<set label="Oct" value="371" />',
                '</chart>'
            ].join(',');
        }
		else
			this.chart_data = response;
		
		
        //render flash
        this.renderChart(this.width, this.height);
        //fire when widget loaded or change height
        window.fireEvent('widget_load', this.widgetInfo);
    },
    displayFailure: function(){
    },
    onContentresize: function(event){
        this.refreshMod();
    },
    renderChart: function(x, y)
	{
		if(this.chart_data != null)
		{
			this.myChart = new FusionCharts(homeBase+'lib/FusionCharts/Charts/' + this.swf, 'chart_' + this.id, x, y, '0', '0');
			this.myChart.setDataXML(this.chart_data);
			this.myChart.setTransparent(true);
			this.myChart.render(this.chartBox);
		}
    },
    refreshMod:function()
	{
		this.myChart = null;
		if (typeof(CollectGarbage) === 'function'){
			CollectGarbage();
		}
        var xy = this.getContentSize();
        this.renderChart(xy.x, xy.y-this.errormsg.getSize().y);
        //need to fire widget_load event if it change widget's height
        //window.fireEvent('widget_load');
    },
    onClickHelp: function(){
        var lang_path = modSample2_locLang.getLang();
        var myRequest = new Request({
            url: this.widgetPath+'/help/'+lang_path+'/help.html',
            method: 'get',
            onSuccess: function(responseText){
                var options = {
                    title: modSample2_locLang.label('help') + ' - ' +  modSample2_locLang.label('title'),
                    content: responseText
                };
                new WFModal(options);
            }
        }).send();
    }
});
